using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Drawing;

namespace EVEChatLib
{
    public partial class Lib
    {
        [DllImport("User32.dll", CharSet = CharSet.Auto, EntryPoint = "SendMessage")]
        static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);

        const int WM_VSCROLL = 277;
        const int SB_BOTTOM = 7;

        // Using unmanaged code to scroll the specified textbox
        public void ScrollToEnd(RichTextBox rtb)
        {
            IntPtr ptrWparam = new IntPtr(SB_BOTTOM);
            IntPtr ptrLparam = new IntPtr(0);
            SendMessage(rtb.Handle, WM_VSCROLL, ptrWparam, ptrLparam);
        }

        public string addChatToRtb(RichTextBox rtb, ListBox lb, string[,] chat)
        {
            string text = "";
            string theLastChat = "";

            // Make sure it isn't empty

            if (chat[0,0] != "error")
            {
                // Loop through the array of chat
                for (int i = 0; i < (chat.Length / 2); i++)
                {

                    // Get the player name
                    string playerName = chat[i, 1].Remove(chat[i, 1].IndexOf(" > "));
                    if (playerName != "EVE System")
                    {
                        if (!lb.Items.Contains(playerName))
                        {
                            lb.Items.Add(playerName);
                        }
                    }

                    string chatText = chat[i, 1].Remove(0, chat[i, 1].IndexOf(" > ") + 3);

                    if (i == ((chat.Length / 2) - 1))
                    {
                        theLastChat = chat[i, 1];
                    }

                    // Append the text to the RTB text
                    // Using our own little symbol like thingys so we can detect where we want color
                    text = text + "~#:pName:#~" + playerName + "~#:tText:#~" + " > " + chatText + "\n";
                }
            }

            rtb.AppendText(text);

            rtb.Rtf = rtb.Rtf.Replace("~#:pName:#~", "\\cf1 ");
            rtb.Rtf = rtb.Rtf.Replace("~#:tText:#~", "\\cf2 ");

            return theLastChat;
        }

        public void addColorHeaders(RichTextBox rtb, Color[] colors)
        {
            //Get RTF from richtextbox
            string strRTF = rtb.Rtf;
            rtb.Clear();

            int nameColorR = (int)colors[0].R;
            int nameColorG = (int)colors[0].G;
            int nameColorB = (int)colors[0].B;

            int textColorR = (int)colors[1].R;
            int textColorG = (int)colors[1].G;
            int textColorB = (int)colors[1].B;

            int textColor2R = (int)colors[2].R;
            int textColor2G = (int)colors[2].G;
            int textColor2B = (int)colors[2].B;

            int backgroundColorR = (int)colors[3].R;
            int backgroundColorG = (int)colors[3].G;
            int backgroundColorB = (int)colors[3].B;

            string colorTable = "colortbl;\\red" + nameColorR + "\\green" + nameColorG + "\\blue" + nameColorB + ";\\red" + textColorR + "\\green" + textColorG + "\\blue" + textColorB + ";\\red" + textColor2R + "\\green" + textColor2G + "\\blue" + textColor2B + ";\\red" + backgroundColorR + "\\green" + backgroundColorG + "\\blue" + backgroundColorB + ";}";

            int iCTableStart = strRTF.IndexOf("colortbl;");

            if (iCTableStart != -1)
            {
                int iCTableEnd = strRTF.IndexOf('}', iCTableStart);

                strRTF = strRTF.Remove(iCTableStart, iCTableEnd - iCTableStart);

                strRTF = strRTF.Insert(iCTableStart, colorTable);
            }
            else 
            {
                int iRTFLoc = strRTF.IndexOf("\\rtf");
            
                int iInsertLoc = strRTF.IndexOf('{', iRTFLoc);

                if (iInsertLoc == -1) iInsertLoc = strRTF.IndexOf('}', iRTFLoc) - 1;
              
                strRTF = strRTF.Insert(iInsertLoc, "{\\" + colorTable);
            }

            rtb.Rtf = strRTF;
        }
    }
}
